package com.example.auction.controller;

import com.example.auction.entity.Item;
import com.example.auction.entity.dto.ItemDTO;
import com.example.auction.entity.vo.ItemVO;
import com.example.auction.mapper.ItemMapper;
import com.example.auction.result.Result;
import com.example.auction.service.ItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/*
拍卖controller
* */
@RestController
@RequestMapping("/item")
@Api(tags = "拍卖相关接口")
public class ItemController {
    @Autowired
    private ItemService itemService;
    @Autowired
    private RedisTemplate redisTemplate;
    /*
    * 创建拍卖接口
    * */
    @PostMapping("/createItem")
    @ApiOperation("新建拍卖")
    public Result<String> createItem(ItemDTO itemDTO){
        itemService.createItem(itemDTO);
        return Result.success("拍卖新建成功");
    }

    /*
     * 根据id查询拍卖
     * */
    @GetMapping("/{id}")
    @ApiOperation("根据Id查询拍卖（预览拍卖）")
    public Result<ItemVO> getItemById(@PathVariable Long id) {
    /*
        看需求
        //构造redis的key，规则：item_拍卖Id
        String key ="dish_" + ItemId;
        //查询redis中是否存在
        if (redisTemplate.hasKey(key) != null) {
            //如果存在直接返回
            return Result.success((ItemVO) redisTemplate.opsForValue().get(key));
        }
        //如果不存在,查询数据库,将查询到的数据放入redis中*/
        Item item = itemService.getItemById(id);
        ItemVO itemVO = new ItemVO();
        BeanUtils.copyProperties(item, itemVO);
        return Result.success(itemVO);
    }

    /*
    *  根据id删除拍卖
    * */

}
